package ysq.xyg.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.dao.ArticleDao;
import ysq.xyg.model.Article;
import ysq.xyg.service.ArticleService;

import javax.annotation.Resource;
import java.util.List;

@Service("articleService")
public class ArticleServiceImpl extends BaseServiceImpl<Article> implements ArticleService {

    @Resource
    private ArticleDao articleDao;
    @Autowired
    public void setArticleDao(ArticleDao articleDao) {
        this.baseDao = articleDao;
    }

    @Override
    public PageInfo<Article> pageInfo(Article article, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy("sort desc");
        // 通用Example查询
        Example example = new Example(Article.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(article.getStatus())) {
            criteria.andEqualTo("status", article.getStatus());
        }
        if (StringUtils.isNotEmpty(article.getTitle())) {
            criteria.andLike("title", "%" + article.getTitle() + "%");
        }
        List<Article> list = articleDao.selectByExample(example);
        return new PageInfo<Article>(list);
    }

}
